package com.cpfmat.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cpfmat.common.Contains;
import com.cpfmat.entity.SystemMenu;
import com.cpfmat.service.IArticleService;
import com.cpfmat.service.ISystemMenuService;
import com.cpfmat.vo.CustomArticle;

@Controller
@RequestMapping("/mobile")
public class MobileController {
	@Autowired
	private IArticleService articleService;
	@Autowired
	private ISystemMenuService menuService;

	@RequestMapping(path = "/list")
	public ModelAndView list(@RequestParam(name = "keyword", required = false) String keyword,
			@RequestParam(name = "pageNum", defaultValue = "1") int pageNum) {
		ModelAndView view = new ModelAndView("/mobileAppend");
		
		Page<CustomArticle> page = articleService.getAllPage(keyword, pageNum, Contains.PAGE_SIZE);
		
		view.addObject("page",page);
		
		return view;
	}
	
	@RequestMapping(path = "/coordination")
	public ModelAndView coordination(@RequestParam(name = "keyword", required = false) String keyword,
			@RequestParam(name = "pageNum", defaultValue = "1") int pageNum) throws Exception {
		String type="coordination";
		ModelAndView view = new ModelAndView("/mobileAppend");
		
		EntityWrapper<SystemMenu> menu = new EntityWrapper<SystemMenu>();
		menu.eq("`KEY`", type);
		menu.eq("DEL_FLAG", 0);
		SystemMenu selectOne = menuService.selectOne(menu);
		String parentColumnId = null;
		if(selectOne != null){
			parentColumnId = selectOne.getSeqId();
		}
		if(parentColumnId == null){
			throw new Exception("找不到" + type + "对应的菜单信息");
		}
		
		Page<CustomArticle> page = articleService.getPage(keyword, null, parentColumnId, pageNum, Contains.PAGE_SIZE);
		
		view.addObject("page",page);
		
		return view;
	}

	@RequestMapping(path = "/contact")
	public ModelAndView contact() {
		ModelAndView view = new ModelAndView("/column/contact");

		return view;
	}
	
	@RequestMapping(path = "/policy")
	public ModelAndView policy(@RequestParam(name = "keyword", required = false) String keyword,
			@RequestParam(name = "pageNum", defaultValue = "1") int pageNum) {
		String type = "policy";
		ModelAndView view = new ModelAndView("/mobileAppend");
		SystemMenu systemMenu = Contains.menuKeyMap.get(type);
		
		if(systemMenu != null){
			Page<CustomArticle> page = articleService.getPage(keyword, systemMenu.getSeqId(), null, pageNum, Contains.PAGE_SIZE);
			view.addObject("page",page);
		}
		
		return view;
	}
	
	@RequestMapping(path = "/column")
	public ModelAndView column(@RequestParam(name = "keyword", required = false) String keyword,
			@RequestParam(name = "pageNum", defaultValue = "1") int pageNum) {
		String type = "column";
		ModelAndView view = new ModelAndView("/mobileAppend");
		SystemMenu systemMenu = Contains.menuKeyMap.get(type);
		
		if(systemMenu != null){
			Page<CustomArticle> page = articleService.getPage(keyword, systemMenu.getSeqId(), null, pageNum, Contains.PAGE_SIZE);
			view.addObject("page",page);
		}
		
		return view;
	}
	
	@RequestMapping(path = "/news")
	public ModelAndView news(@RequestParam(name = "keyword", required = false) String keyword,
			@RequestParam(name = "pageNum", defaultValue = "1") int pageNum) {
		String type = "news";
		ModelAndView view = new ModelAndView("/mobileAppend");
		SystemMenu systemMenu = Contains.menuKeyMap.get(type);
		
		if(systemMenu != null){
			Page<CustomArticle> page = articleService.getPage(keyword, systemMenu.getSeqId(), null, pageNum, Contains.PAGE_SIZE);
			view.addObject("page",page);
		}
		
		return view;
	}
	
	@RequestMapping(path = "/notice")
	public ModelAndView notice(@RequestParam(name = "keyword", required = false) String keyword,
			@RequestParam(name = "pageNum", defaultValue = "1") int pageNum) {
		String type = "notice";
		ModelAndView view = new ModelAndView("/mobileAppend");
		SystemMenu systemMenu = Contains.menuKeyMap.get(type);
		
		if(systemMenu != null){
			Page<CustomArticle> page = articleService.getPage(keyword, systemMenu.getSeqId(), null, pageNum, Contains.PAGE_SIZE);
			view.addObject("page",page);
		}
		
		return view;
	}
	
	@RequestMapping(path = "/project")
	public ModelAndView project(@RequestParam(name = "keyword", required = false) String keyword,
			@RequestParam(name = "pageNum", defaultValue = "1") int pageNum) {
		String type = "project";
		ModelAndView view = new ModelAndView("/mobileAppend");
		SystemMenu systemMenu = Contains.menuKeyMap.get(type);
		
		if(systemMenu != null){
			Page<CustomArticle> page = articleService.getPage(keyword, systemMenu.getSeqId(), null, pageNum, Contains.PAGE_SIZE);
			view.addObject("page",page);
		}
		
		return view;
	}
	
	@RequestMapping(path = "/gain")
	public ModelAndView gain(@RequestParam(name = "keyword", required = false) String keyword,
			@RequestParam(name = "pageNum", defaultValue = "1") int pageNum) {
		String type = "gain";
		ModelAndView view = new ModelAndView("/mobileAppend");
		SystemMenu systemMenu = Contains.menuKeyMap.get(type);
		
		if(systemMenu != null){
			Page<CustomArticle> page = articleService.getPage(keyword, systemMenu.getSeqId(), null, pageNum, Contains.PAGE_SIZE);
			view.addObject("page",page);
		}
		
		return view;
	}
	
	@RequestMapping(path = "/need")
	public ModelAndView need(@RequestParam(name = "keyword", required = false) String keyword,
			@RequestParam(name = "pageNum", defaultValue = "1") int pageNum) {
		String type = "need";
		ModelAndView view = new ModelAndView("/mobileAppend");
		SystemMenu systemMenu = Contains.menuKeyMap.get(type);
		
		if(systemMenu != null){
			Page<CustomArticle> page = articleService.getPage(keyword, systemMenu.getSeqId(), null, pageNum, Contains.PAGE_SIZE);
			view.addObject("page",page);
		}
		
		return view;
	}
	
	@RequestMapping(path = "/collection")
	public ModelAndView collection(@RequestParam(name = "keyword", required = false) String keyword,
			@RequestParam(name = "pageNum", defaultValue = "1") int pageNum) {
		String type = "collection";
		ModelAndView view = new ModelAndView("/mobileAppend");
		SystemMenu systemMenu = Contains.menuKeyMap.get(type);
		
		if(systemMenu != null){
			Page<CustomArticle> page = articleService.getPage(keyword, systemMenu.getSeqId(), null, pageNum, Contains.PAGE_SIZE);
			view.addObject("page",page);
		}
		
		return view;
	}
}
